<?php

/**
 *  获取系统所有分类标签
 * @return type
 */
function getCategoryLabel() {

    $cache_key = 'caregory_label_list';
    $cache_key2 = 'caregory_label_list_key_value';
    $label = S($cache_key);
    if (!$label) {

        $label_list = array();
        $where = array();
        $where['l.status'] = 1;
        $list = M('CategoryLabel')->field('c.name as category_name,l.*')->where($where)->order('l.display desc')->alias('l')->join('LEFT JOIN __CATEGORY__ as c ON c.id=l.category_id')->select();
        $label = array();
        foreach ($list as $info) {
            if (!isset($label[$info['category_id']])) {
                $label[$info['category_id']] = array(
                    'category_id' => $info['category_id'],
                    'category_name' => $info['category_name'],
                );
            }
            $label_list[$info['id']] = $info['name'];
            $label[$info['category_id']]['label_list'][] = getArrayToKey(array($info), array('name', 'id'))[0];
        }
        ksort($label_list);
        S($cache_key2, $label_list, 300); //储存标签的键值对
        S($cache_key, $label, 300); //储存包含父栏目的标签列表
    }
    return $label;
}

function getLabelNameById($label_id) {
    $cache_key = 'caregory_label_list_key_value';
    $label_list = S($cache_key);
    if (!$label_list) {
        getCategoryLabel(); //生成标签列表
        $label_list = S($cache_key);
    }
    return (string) $label_list[$label_id];
}
